<?php
namespace Kuoqi;
use Kuoqi\Worker\Logs;
use Kuoqi\Config;

/**
 * Class Core worker内核部分，用于worker的相关信息 cli模式的核心模块
 * @package Kuoqi\Worker
 * User kuoqi
 * Datetime 2022/3/17 16:05
 * Remark
 */
class Core extends Worker {

    //入口信息
    protected $index = '';
    //当前执行命令部分信息
    protected $command = '';




    //命令绑定路由信息
    protected $routes = array();

    protected $argc = 0;//输入的参数数量信息

    //worker的实际操作
    protected $server = null;
    protected $lock = '';//lock文件
    protected $server_address = '127.0.0.1';//监听服务地址
    protected $server_port = '6519';//监听服务端口

    protected $interval = 0;

    /**
     * Core constructor. 初始化
     * 初始化参数输入模式
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 结构初始化
     * User kuoqi
     * Datetime 2022/3/24 14:49
     * Remark
     */
    public function init(){
        $this->server_address = Config::get('WORKER_ADDR');
        $this->server_port = Config::get('WORKER_PORT');
        $tmp = md5($this->server_address.'_'.$this->server_port);
        //tempnam.
        //resource tmpfile( void)
        $this->lock = sys_get_temp_dir().DIRECTORY_SEPARATOR.'kuoqiworker-'.$tmp.'.tmp';
    }

    /**
     * worker启动的部分，也是worker初始化的一部分
     * User kuoqi
     * Datetime 2022/3/17 16:05
     * Remark
     */
    public function running()
    {
        //初始化基础信息
        $this->init();
        //执行启动程序
        $this->server = new \Kuoqi\Worker\Server($this->server_address,$this->server_port);
        $this->initInput();//输出信息的初始化
        if($this->command=='start'){
            //如果这个为启动命令的话，执行这个操作
            $this->say('running..');
            $pid = pcntl_fork();
            if($pid){
                $this->runAll();//启动进程脚本信息,当做本身的启动，当前对象为一个worker
                exit($pid);
            }else{
                //副进程
                sleep(1);
                $client = new \Kuoqi\Worker\Client($this->server_address,$this->server_port);
                foreach ($this->routes as $item){
                    //这里使用工作命令信息
                    $client->connect();
                    if(!$client->isValied()){
                        $this->say($client->getError());
                        //如果发现异常，直接退出
                        $this->say('send command error '.sprintf('start -r %s',$item));
                        return;
                    }
                    $this->say('send command '.sprintf('start -r %s',$item));
                    $client->command(sprintf('start -r %s',$item));
                    $client->close();
                }
                $this->say('run something');
            }
        }else{
            $this->runAll();//启动进程脚本信息,当做本身的启动，当前对象为一个worker
        }
    }

    /**
     * 执行run方法
     * User kuoqi
     * Datetime 2022/3/27 13:48
     * Remark
     */
    public function run(){
        $this->server->listen();//服务运行
    }

    //绑定路由信息，进入worker
    public function bindRoute($route){
        //$this->server->bindRoute($route);
        $this->routes[] = $route;
    }


    //输出到屏幕的内容
    public function msg($msg){ echo $msg.PHP_EOL;}

    public function __destruct(){

    }
}

